Welcome

Instructor


Colin Rundel

Associate Professor of the Practice

Department of Statistical Science

Duke University

rundel.github.io

Teaching Assistants

  • Jeremy Allen (Posit)
  • Joe Cheng

Introduce yourself

We wont go around the room, but take the next couple of minutes to introduce yourself to your neighbors.

Some suggested topics,

  • Name

  • Where you are coming from

  • Why you are interested in learning Shiny

03:00

Workshop materials



Schedule

Time Activity
09:00 - 09:30 Welcome
09:30 - 10:30 Intro to Shiny
10:30 - 11:00 Coffee break
11:00 - 12:30 Basic Reactivity
12:30 - 13:30 Lunch break
13:30 - 15:00 Observers & reactives
15:00 - 15:30 Coffee break
15:30 - 17:00 Themeing & Publishing

WiFi



Network: Posit Conf 2024

Password: conf2024

Code of Conduct

All details are available at https://posit.co/code-of-conduct/. Please review them carefully.

You can report Code of Conduct violations in person (any posit::conf staff ), by email (codeofconduct@posit.co), or by phone (844-448-1212). Please see the policy linked above for contact information.

Other useful info

  • There are gender-neutral bathrooms located are among the Grand Suite Bathrooms.

  • There are two meditation/prayer rooms: Grand Suite 2A and Grand Suite 2B.

    • Open Sunday - Tuesday 7:30 a.m. - 7:00 p.m., Wednesday 8:00 a.m. - 6:00 p.m.
  • The lactation room is located in Grand Suite 1.

    • Open Sunday - Tuesday 7:30 a.m. - 7:00 p.m., Wednesday 8:00 a.m. - 6:00 p.m.
  • Participants who do not wish to be photographed have red lanyards; please note everyone’s lanyard colors before taking a photo and respect their choices.

Asking for help (Stickies)

I’m stuck

I’m working

I’m done


I have a general question

Other communication (Discord)

You should have received an email with an invitation and instructions for joining the conference’s discord server.

This workshop has a private channel under Workshops,

#getting-started-with-shiny-r

This is a great place to ask questions, post resources, memes, or most anything else before, during, and after the workshop.

Computational Environment

RStudio Cloud

You can use the following link to join the workshops RStudio cloud space,

bit.ly/conf2024_shiny_cloud


Once you have joined you can then select the get-started-shiny assignment,

which should then create a copy of all materials and launch a cloud session for you.

Cloud session

If everything is working you should see something very close to the following,

File organization

Project root:

  • slides/ - all slides and related materials

  • demos/ - sample code for each demo

  • exercises/ - starter code for each exercise

  • exercises/solutions/ - sample solution code for each exercise

  • data/ - example data sets used in demos and exercises

Introducing Shiny

Shiny

Shiny is an R package that makes it easy to build interactive web apps straight from R. You can host standalone apps on a webpage or embed them in R Markdown documents or build dashboards. You can also extend your Shiny apps with CSS themes, htmlwidgets, and JavaScript actions.

Shiny App


Server

+


Client / Browser

+ +

Anatomy of an App

library(shiny)

ui = list()

server = function(input, output, session) {}

shinyApp(ui = ui, server = server)

Demo 01 - A Basic Example

demos/demo01.R

library(tidyverse)
library(shiny)

d = readr::read_csv("data/weather.csv")

ui = fluidPage(
  titlePanel("Temperatures at Major Airports"),
  sidebarLayout(
    sidebarPanel(
      radioButtons(
        "name", "Select an airport",
        choices = c(
          "Seattle-Tacoma",
          "Raleigh-Durham",
          "Houston Intercontinental",
          "Denver",
          "Los Angeles",
          "John F. Kennedy"
        )
      ) 
    ),
    mainPanel( 
      plotOutput("plot")
    )
  )
)

server = function(input, output, session) {
  output$plot = renderPlot({
    d |>
      filter(name %in% input$name) |>
      ggplot(aes(x=date, y=temp_avg)) +
      geom_line()
  })
}

shinyApp(ui = ui, server = server)

Your turn - Exercise 01

Copy the code from the previous slide (or open exercises/ex01.R) and try running it using the button.

Check that you are able successfully run the shiny app and are able to interact with it by picking a new airport.

  • If everything is working try modifying the code (e.g. try adding or removing a city from radioButtons()).

  • What happens if you add a city that is not in the weather.csv data set to the radio button input?

05:00

Troubleshooting

A couple of quick tips:

  • If the app can’t find the data, make sure you have opened the workshop’s RStudio project

  • If you are not using Posit cloud make sure you have the latest versions of shiny and tidyverse installed

  • If you are stuck, ask a neighbor for help and/or raise your hand and myself or a TA will come by

UI Basics

Multi-row layout

Other layouts

Input Widgets

A brief widget tour

rundel.shinyapps.io/widgets/

Your turn - Exercise 02

We’ve just seen a number of alternative input widgets, starting from the code in exercises/ex02.R try changing the radioButton() input to something else.

What happens if you use an input capable of selecting multiple values

  • e.g. checkboxGroupInput()

  • or selectInput(multiple = TRUE)?

06:00